
var tempcontrol = null;
var static_label = false;
var dynamic_text = false;
var child_print = false;
var print_image = false;
var dragapproved = false;
var curstaticlabel = null;
/**
 * Ajax提交数据,并改变id=changeContentId的innerHTML值
 * url:提交的地址
 * changeContentId:要改变的目标ID
 * execScript:是否执行页面的js
 * para:参数
 */
var _changeThisContent = function(url,changeContentId,execScript,para){
	var _para=arguments.length == 4? para:'';
	 new _AJAX.Request(url, {method: 'post',parameters: _para, onComplete:function(r) {
			$(changeContentId).innerHTML = r.responseText;		
			$(changeContentId).innerHTML.evalScripts();
			afterChangeContent();			
	 }});
}
function afterChangeContent(){
}
//从某个字符串中提取需要的信息，字符串必须满足以下样式  
//样式的写法是"param1: value1; param2: value3; ..."
//source 要查找的信息字符串
//name   要提取的属性名(param)
function getAttribute(source, name){
  var reg = new RegExp("(^|;|\\s)"+ name +"\\s*=\\s*([^;]*)(\\s|;|$)", "i"); 
  if (reg.test(source))
    return RegExp.$2.replace(/[\x0f]/g, ";"); return "";
}
//设计区调用类
function PrintDesignContent() {	
	this.eventsource;
	this.x=0;
	this.y=0;
	this.popeventsource = "";
	this.temp1 = 0;
	this.temp2 = 0;
	this.ismousedown = false;	
}
var pdc = new PrintDesignContent();

//停止拖动
PrintDesignContent.prototype.nodrags = function(){
	dragapproved = false;
	if(this.eventsource && this.eventsource.control =='1') {
		this.eventsource.style.display = 'none';
		createLabel();
	} else if(this.eventsource && (this.eventsource.statictext == '1' || 
									this.eventsource.dynamictext == '1' || 
									this.eventsource.childprint == '1' || 
									this.eventsource.printimage == '1')){
		controlPositionChange(this.eventsource,this.eventsource.parentElement);
		$('line_l').from = '0,0';
		$('line_l').to = '0,0';
		$('line_v').from = '0,0';
		$('line_v').to = '0,0';
	}
}
//拖动中
PrintDesignContent.prototype.moves = function (){
	if (event.button == 1 && dragapproved){//alert(this.eventsource);
		var newleft = this.temp1 + event.clientX - this.x;
		var newtop = this.temp2 + event.clientY - this.y;
		this.eventsource.style.pixelLeft = newleft;
		this.eventsource.style.pixelTop = newtop;
		drawtempline(this.eventsource);
		return false;
	}
}
//拖动中画对齐线
var drawtempline = function (obj){
	//计算控件所在区域的所有控件的上下左右坐标
	var pobj  =  obj.parentElement;
	if(pobj.id.indexOf('title') == -1) return;
	var allcons = pobj.getElementsByTagName('input');
	var allimages = pobj.getElementsByTagName('img');
	var tt = 0;
	for(var i = allcons.length;i < (allcons.length + allimages.length);i++) {
		allcons[i] = allimages[tt];
		tt++;
	}
	var y1 = obj.style.pixelTop;//当前控件顶部
	var y2 = obj.style.pixelTop + obj.offsetHeight;//当前控件底部	
	var x1 = obj.style.pixelLeft;//当前控件左部
	var x2 = obj.style.pixelLeft + obj.offsetWidth;//当前控件右部
	var b1 = false;//顶部对齐标识
	var b2 = false;//底部对齐标识
	var b3 = false;//左边对齐标识
	var b4 = false;//右边对齐标识
	var level_x = 0;//水平横线纵标标
	var obj_x = 0;//当前控件顶部位置
	var aplomb_y =0;//垂直横线横坐标
	var obj_y = 0;//当前控件横坐标
	var num = 4;
	var b = false;
	//判断是否在区域顶部或底部
	var yp1 = 0;//当前区域顶部
	var yp2 = yp1+pobj.offsetHeight;//当前区域底部
	var xp1 = parseInt(pobj.style.left.gsub('px','')) + parseInt(pobj.parentElement.dleft);//区域左部
	var xp2 = xp1 + pobj.offsetWidth - parseInt(pobj.parentElement.dleft) - parseInt(pobj.parentElement.dright);//区域右部
	//控件顶部与区域顶部齐
	if(Math.abs(yp1 -y1) <= num) {
		b1 = true;			
		level_x = 0;
		obj_x = level_x;
	}
	//控件底部与区域底部齐
	if(Math.abs(yp2 -y2) <= num) {		
		b2 = true;			
		level_x = pobj.offsetHeight;
		obj_x = level_x-obj.offsetHeight;
	}
	//控件左部与区域左部齐
	if(Math.abs(xp1 -x1)<= num) {		
		b3 = true;			
		aplomb_y = xp1;
		obj_y = xp1;
	}
	//控件右部与区域右部齐
	if(Math.abs(xp2 -x2)<= num) {		
		b4 = true;			
		aplomb_y = xp2;
		obj_y = aplomb_y - obj.offsetWidth;
	}
	for(var i =0;i < allcons.length;i++) {
		if(allcons[i] == obj) {continue;}
		var y3 = allcons[i].style.pixelTop;//控件的上边缘
		var y4 = (allcons[i].style.pixelTop+ allcons[i].offsetHeight);//控件下边缘
		var x3 = allcons[i].style.pixelLeft;//控件的左边缘
		var x4 = allcons[i].style.pixelLeft + allcons[i].offsetWidth;//控件的右边缘
		//当前移动控件(顶部)在其它控件顶部上下num个象素位置时
		if(!b1 && (Math.abs(y3 -y1) <=num)) {//alert(allcons[i].outerHTML);
			b1 = true;			
			level_x = parseInt(allcons[i].style.top.gsub('px',''));
			obj_x = level_x;			
		}
		//当前移动控件(顶部)在其它控件底部上下num个象素位置时
		if(!b1 && (Math.abs(y4 -y1) <= num)) {
			b1 = true;			
			level_x = parseInt(allcons[i].style.top.gsub('px','')) + allcons[i].offsetHeight ;
			obj_x = level_x;
		}
		//当前移动控件(底部)在其它控件顶部上下num个象素位置时
		if(!b2 && (Math.abs(y3 -y2) <=num)) {
			b2 = true;			
			level_x = parseInt(allcons[i].style.top.gsub('px',''));
			obj_x = level_x - obj.offsetHeight;
		}
		//当前移动控件(底部)在其它控件底部上下num个象素位置时
		if(!b2 && (Math.abs(y4 -y2) <=num)) {
			b2 = true;			
			level_x = parseInt(allcons[i].style.top.gsub('px','')) + allcons[i].offsetHeight ;
			obj_x = level_x - obj.offsetHeight;
		}
	}
		//88888888888888888888888888888888888888888888888888888888888888888
		//当前移动控件(左部)在其它控件左部左右num个象素位置时
	allcons = pobj.parentElement.getElementsByTagName('input');
	for(var i =0;i < allcons.length;i++) {
		if(allcons[i] == obj) {continue;}
		var y3 = allcons[i].style.pixelTop;//控件的上边缘
		var y4 = (allcons[i].style.pixelTop+ allcons[i].offsetHeight);//控件下边缘
		var x3 = allcons[i].style.pixelLeft;//控件的左边缘
		var x4 = allcons[i].style.pixelLeft + allcons[i].offsetWidth;//控件的右边缘
		if(!b3 && (Math.abs(x1 -x3) <=num)) {
			b3 = true;			
			aplomb_y = parseInt(allcons[i].style.left.gsub('px',''));
			obj_y = aplomb_y;
		}
		//当前移动控件(左部)在其它控件左部左右num个象素位置时
		if(!b3 && (Math.abs(x1 -x4) <=num)) {
			b3 = true;			
			aplomb_y = parseInt(allcons[i].style.left.gsub('px',''))+allcons[i].offsetWidth ;
			obj_y = aplomb_y;
		}
		//当前移动控件(右部)在其它控件右部左右num个象素位置时
		if(!b4 && (Math.abs(x2 -x3) <=num)) {
			b4 = true;			
			aplomb_y = parseInt(allcons[i].style.left.gsub('px',''));
			obj_y = aplomb_y - obj.offsetWidth;
		}
		//当前移动控件(右部)在其它控件左部左右num个象素位置时
		if(!b4 && (Math.abs(x2 -x4) <=num)) {
			b4 = true;			
			aplomb_y = parseInt(allcons[i].style.left.gsub('px',''))+allcons[i].offsetWidth;
			obj_y = aplomb_y - obj.offsetWidth;
		}
	}	
	
	if(b1 || b2) {//alert(b1);alert(b2);
		$('line_l').from ='0,' + (pobj.offsetTop +level_x);
		$('line_l').to = pobj.clientWidth +',' + (pobj.offsetTop +level_x);
		obj.style.top = obj_x;
	} else {
		$('line_l').from = '0,0';
		$('line_l').to = '0,0';		
	}
	if(b3 || b4) {
		$('line_v').from = (aplomb_y)+',0';
		$('line_v').to = (aplomb_y)+',' + (pobj.parentElement.offsetHeight);
		obj.style.left = obj_y;
	}else {		
		$('line_v').from = '0,0';
		$('line_v').to = '0,0';
	}
}

//开始拖动
PrintDesignContent.prototype.drags = function (){
	if (event.button != 1)
		return;
	var obj = event.srcElement;		
	if ((obj.tagName.toLowerCase() == 'input' && obj.type == 'text') || obj.tagName.toLowerCase() == 'img') {
		dragapproved = true;
		this.eventsource = obj;
		this.temp1 = this.eventsource.style.pixelLeft;
		this.temp2 = this.eventsource.style.pixelTop;
		//if(objRect.tagName.toLowerCase() == 'input') 
		this.x = event.clientX;
		this.y = event.clientY;//alert(this.x);			
		
	}  else if(obj.tagName.toLowerCase() == 'button' && obj.controldiv =='1' ) {//创建控件拖动(静态文本)
		dragapproved = true;
		this.eventsource = tempcontrol;
		this.temp1 = this.eventsource.style.pixelLeft;
		this.temp2 = this.eventsource.style.pixelTop;
		//if(objRect.tagName.toLowerCase() == 'input') 
		this.x = event.clientX;
		this.y = event.clientY;
		static_label = true;
	} else if(obj.tagName.toLowerCase() == 'button' && obj.controldiv =='2' ) {//创建控件拖动(动态)
		dragapproved = true;
		this.eventsource = tempcontrol;
		this.temp1 = this.eventsource.style.pixelLeft;
		this.temp2 = this.eventsource.style.pixelTop;
		//if(objRect.tagName.toLowerCase() == 'input') 
		this.x = event.clientX;
		this.y = event.clientY;
		dynamic_text = true;
	} else if(obj.tagName.toLowerCase() == 'button' && obj.controldiv =='3' ) {//创建控件拖动(明细)
		dragapproved = true;
		this.eventsource = tempcontrol;
		this.temp1 = this.eventsource.style.pixelLeft;
		this.temp2 = this.eventsource.style.pixelTop;
		//if(objRect.tagName.toLowerCase() == 'input')
		this.x = event.clientX;
		this.y = event.clientY;
		child_print = true;
	} else if(obj.tagName.toLowerCase() == 'button' && obj.controldiv =='4' ) {//创建控件拖动(图片)
		dragapproved = true;
		this.eventsource = tempcontrol;
		this.temp1 = this.eventsource.style.pixelLeft;
		this.temp2 = this.eventsource.style.pixelTop;
		//if(objRect.tagName.toLowerCase() == 'input') 
		this.x = event.clientX;
		this.y = event.clientY;
		print_image = true;
	}
}

//上下左右移动键事件
PrintDesignContent.prototype.keydown = function (){
	if(this.eventsource && (this.eventsource.statictext == '1' || 
		this.eventsource.dynamictext == '1' || 
		this.eventsource.childprint == '1' || 
		this.eventsource.printimage == '1')) {
		var key = event.keyCode;
		if(key == 38) {//上
			this.eventsource.style.top = (parseInt(this.eventsource.style.top.gsub('px','')) -1);
			drawtempline(this.eventsource);
		} else if(key == 37) {//左
			this.eventsource.style.left = (parseInt(this.eventsource.style.left.gsub('px','')) -1);
			drawtempline(this.eventsource);
		} else if(key == 39) {//右
			this.eventsource.style.left = (parseInt(this.eventsource.style.left.gsub('px','')) +1);
			drawtempline(this.eventsource);
		} else if(key ==  40) {//下
			this.eventsource.style.top = (parseInt(this.eventsource.style.top.gsub('px','')) +1);
			drawtempline(this.eventsource);
		} else if(key == 46) {//删
			if(confirm('确定要删除此控件吗?')) {
			this.eventsource.parentElement.removeChild(this.eventsource);
			$('div32').innerHTML = '';
			}
		}
		controlPositionChange(this.eventsource,this.eventsource.parentElement);
		//alert(key);
	}
}
//设置左右页边距线条
PrintDesignContent.prototype.drawline = function(obj,type) {
	var pe = obj.parentElement;
	if(type == 1) {
		var str =  (pe.dleft) + ',0';//pe.dleft+',-'+(pe.dheight - pe.dbottom);//alert(pe.dheight);
		obj.from = str;
		obj.to = (pe.dleft)+','+pe.offsetHeight;
	} else {
		obj.from = (pe.offsetWidth- pe.dright)+',0';
		obj.to = (pe.offsetWidth- pe.dright) +','+pe.offsetHeight;
	}
}

//div拖动完后事件
PrintDesignContent.prototype.divchanged = function (num) {	
	var obj = $('title'+num);	
	if(obj) {//alert(event.y + '  ' +obj.offsetTop);
		//if(Math.abs(event.y - obj.offsetTop) < 5) return;alert();
		obj.style.width = '100%';		
		var objhe = obj.offsetHeight;//alert(num+'   '+objhe);
		if(obj.clientHeight < 20) objhe= 20;
		//if(obj.oldheight =='')obj.oldheight=0;
		//调整设计区的高度		
		//alert(obj.oldheight +'            '+ objhe);
		var adjustheight = 0;
		var curhe = 0;
		obj.style.top = obj.oldtop + 'px';
		if(obj.oldheight >= objhe) {
			adjustheight = eval(obj.oldheight - objhe);
			curhe = (parseInt(obj.parentElement.dheight) - adjustheight);	
			setPrintAreaHeight(parseInt(num),-1,adjustheight);
		}else if(obj.oldheight <= objhe){
			adjustheight = eval(objhe - obj.oldheight);
			curhe = (parseInt(obj.parentElement.dheight) + adjustheight);	
			setPrintAreaHeight(parseInt(num),1,adjustheight);
		}
		
		obj.parentElement.style.height = curhe+'px';
		obj.parentElement.dheight=curhe;
		//调整左右页边距
		try{this.drawline($('line1'),1);}catch(e){}
		try{this.drawline($('line2'),2);}catch(e){}
		obj.style.height = objhe+'px';	
		obj.oldheight = objhe;		
		obj.oldtop = obj.offsetTop;
	}
}
//设置打印分区文字
PrintDesignContent.prototype.drawtitle = function(obj) {
	var he =(obj.clientHeight - 22)/2;		
	return he;	
}

//设置打印板块的高度
function setPrintAreaHeight(curnum,type,value) {
	try{
	if(type == 1) {//增加		
		for(var i = (curnum+1);i < 8;i++) {
			if($('title'+i)) {
				$('title'+i).style.top = parseInt($('title'+i).style.top.gsub('px','')) + value;
				$('title'+i).oldtop = $('title'+i).offsetTop;
			}
		}
	} else if(type == -1) {
		for(var i = (curnum+1);i < 8;i++) {
			if($('title'+i)) {
				$('title'+i).style.top = parseInt($('title'+i).style.top.gsub('px','')) - value;
				$('title'+i).oldtop = $('title'+i).offsetTop;
			}
		}
	}
	}catch(e){}
}


//取消指定打印部分
function cancelbound(num) {		
	var obj = $('title'+num);
	if(obj) {
		//调整设计区的高度				
		try{
			var adjustheight = obj.offsetHeight;
			var curhe = (parseInt(obj.parentElement.dheight) - adjustheight);		
			obj.parentElement.style.height = curhe+'px';
			obj.parentElement.dheight=curhe;
			obj.oldheight =  obj.offsetHeight;
			setPrintAreaHeight(parseInt(num),-1,adjustheight);
		}catch(e){}
		//调整左右页边距
		try{pdc.drawline($('line1'),1);}catch(e){}
		try{pdc.drawline($('line2'),2);}catch(e){}
		obj.parentElement.removeChild(obj);
	}
}
//添加指定打印部分
function addbound(num,value) {
	if($('title'+num)) {
		$('title'+num).style.height = value+'px';
	} else {
		var top = parseInt($('div22').dtop);
		var str = '';
		if(num == 1){			
			str = '标题';
		} else if(num ==2) {
			top += addHeight(num);
			str = '页头';			
		} else if(num ==3) {
			top += addHeight(num);
			str = '列头';			
		} else if(num == 4) {			
			top += addHeight(num);
			str = '明细';
			
		} else if(num == 5) {			
			top += addHeight(num);
			str = '列尾';
			
		} else if(num == 6) {
			top += addHeight(num);
			str = '页尾';
			
		} else {
			top += addHeight(num);
			str = '摘要';			
		}			
		var content = "<div id='title"+num+"' " +
				"style='border-bottom:0.01mm groove #dddddd;height:"+value+"px;color:#dddddd;"+
				"z-index:100;position:absolute;width:100%;top:"+top+"px;left:0px;padding-top:expression(pdc.drawtitle(this));' "+
				"oncontrolselect='getPros("+num+",\"\");' ondrag='return false' onmove='return false;' align='center' onresizeend='pdc.divchanged("+num+")' "+
				"oldheight='' oncontrolselect='this.oldheight=this.clientHeight' oldtop='"+top+"'>"+str+"</div>";
		//alert(content);
		var tempdiv = document.createElement('div');
		tempdiv.innerHTML = content;
		$('div22').appendChild(tempdiv.firstChild);//alert($('title'+num).oldheight);
		for(var i = (num-1);i >1;i--) {
			if($('title'+i)) {
				$('title'+num).oldtop = eval(parseInt($('title'+i).style.top.gsub('px','')) + $('title'+i).offsetHeight);
				break;
			}
		}
		//obj.oldtop
		$('title'+num).oldheight = 0;
		setPrintAreaHeight(num,1,value);
	}//alert($('title'+num).style.height);
	pdc.divchanged(num);		
}
//计算添加的高度
function addHeight(num) {
	var he = 0;
	for(var i =1;i < num;i++) {
		if($('title'+i)) {
			he = he + $('title'+i).offsetHeight;
		}
	}//alert(he);
	return he;
}
//加载页面属性
function getPros(type) {
	var url = '/'+ xrbs.WEB_APP_NAME + '/print/printPropertys.jsp';	
	var titleband = $('title'+type);//alert(titleband.style.top);
	var para = 'titletype='+type+'&value='+ (titleband?(titleband.offsetHeight):0);
	new _AJAX.Request(url, {method: 'post',parameters: para, onComplete:function(r) {
		$('div12').innerHTML = r.responseText;
		xrBsMask.maskAllchild($('div12'));
	}});
	if(arguments.length ==2) {
		afterclickpros(type,'');
	} else {
		afterclickpros(type);
	}	
}
//加载参数信息
function getParas(pardiv) {
	var url = '/'+ xrbs.WEB_APP_NAME + '/print/parameters.jsp';	
	var para  = 'parameters='+$F('parameters');
	new _AJAX.Request(url, {method: 'post',parameters: para, onComplete:function(r) {		
		var text = r.responseText;
		$(pardiv).innerHTML = text;
		text.evalScripts();		
	}});
	afterclickpros(8);
}
//加载字段信息
function getFields() {
	var url = '/'+ xrbs.WEB_APP_NAME + '/PrintAction.do?method=queryDataFields';	
	var para = 'data=' + $F('querydata');
	para = encodeURI(para);
	para = encodeURI(para);
	new _AJAX.Request(url, {method: 'post',parameters: para, onComplete:function(r) {		
		var text = r.responseText;
		$('div12').innerHTML = text;		
		text.evalScripts();		
	}});
	afterclickpros(9);
}
//添加字段行
function addFieldTr() {
	var name= '';
	var type = '';
	var tr = '<tr>';
	if(arguments.length ==1) {
		pname = arguments[0].split(':')[0];
		type = arguments[0].split(':')[1];
	}
	tr += '<td>'+pname+'</td>';
	tr += '<td>'+type+'</td>';	
	tr += '</tr>';
	new Insertion.Bottom($('paratab').firstChild,tr);
}

//添加参数行
function addParaTr() {
	var pname= '';
	var ptype = '';
	var tr = '<tr>';
	if(arguments.length ==1) {
		pname = arguments[0].split(':')[0];
		ptype = arguments[0].split(':')[1];
	}
	tr += '<td><input style="width:100%" name=pname value="'+pname+'" onblur="_parValue()"></td>';
	tr += '<td>'+getParaType(ptype)+'</td>';
	tr += '<td><input type=button value="-" onclick="delCurTr(this)"></td>';
	tr += '</tr>';
	new Insertion.Bottom($('paratab').firstChild,tr);
}
//得到参数信息
function getParaInfo() {
	var tb = $('paratab');
	var info = '';
	for(var i =2;i < tb.rows.length;i++) {
		var tr = tb.rows[i];
		if(tr.all['pname'].value.replace(/^\s+|\s+$/g,"") != '') {
			info += tr.all['pname'].value.replace(/^\s+|\s+$/g,"")+":" + tr.all['ptype'].value + ';';
		}
	}
	return info;
}
function _parValue() {
	$('parameters').value = getParaInfo();
}
function delCurTr(obj) {
	var tr = obj.parentElement.parentElement;
	$('paratab').deleteRow(tr.rowIndex);
	_parValue();
}
function getParaType(ptype) {
	var sel = '<select name=ptype style="width:100%" onchange="_parValue()">';
	if(ptype == 'java.math.BigDecimal') {
		sel += '<option value=java.lang.String >文本</option>';
		sel += '<option value=java.math.BigDecimal selected>数字</option>';
	} else {
		sel += '<option value=java.lang.String  selected>文本</option>';
		sel += '<option value=java.math.BigDecimal >数字</option>';
	}
	sel += '</select>';
	return sel;
}
//打印页面设置高度调整后事件
function changeHeight(obj) {
	var value = obj.value.gsub('_','');//alert(obj.titletype);
	if(value == '' ||  value == 0) {
		cancelbound(obj.titletype);
	}else {
		if(value < 20) {
			alert('设置值只能是大于20像素或为0');
			obj.focus();
			obj.select();
		} else {
			addbound(obj.titletype,value);
		}
	}
}
//显示页面设置
function getPage(num) {
	var url = '/'+ xrbs.WEB_APP_NAME + '/print/pagePropertys.jsp';	
	var para = 'pageheight='+$('div22').pheight;
	para += '&pagewidth='+$('div22').dwidth;
	para += '&pagetop='+$('div22').dtop;
	para += '&pageleft='+$('div22').dleft;
	para += '&pageright='+$('div22').dright;
	para += '&pagebottom='+$('div22').dbottom;
	new _AJAX.Request(url, {method: 'post',parameters: para, onComplete:function(r) {
		$('div12').innerHTML = r.responseText;
		xrBsMask.maskAllchild($('div12'));
	}});
	afterclickpros(num);
}
//页面属性点击后事件
function afterclickpros(num) {
	for(var i =0;i < 10;i++) {
		var obj = $('pro'+i);
		if(i == num) {
			obj.style.color = '#ff0000';
			if(arguments.length ==1) {
			var bandobj = $('title'+i);
				if(bandobj) {
					bandobj.setActive();
					bandobj.blur();
				}
			}
		} else {
			obj.style.color = '#0046d5';
		}
	}
}
//页面设置后事件
function changePage(obj) {
	var pagetype = obj.pagetype;
	var value = obj.value.gsub('_','');
	if(1==pagetype) {//宽
		$('div22').style.width = value+'px';
		$('div22').dwidth = value;
		try{pdc.drawline($('line1'),1);}catch(e){}
		try{pdc.drawline($('line2'),2);}catch(e){}
	} else if(2==pagetype) {//高
		$('div22').pheight = value;
	} else if(3 == pagetype) {//上
		/*if(value < 20) {
			alert('设置值只能是大于20像素');
			obj.focus();
			obj.select();
		} else {*/
			var oldv = parseInt($('div22').dtop);	
			$('div22').dtop = value;			
			var newv = parseInt(value);		
			var he = 0;
			//alert($('div22').offsetHeight);
			var curhe = 0;
			if(oldv != newv && oldv - newv > 0)  {//减少
				he = (oldv - newv);
				curhe = (parseInt($('div22').dheight) -he);
				setPrintAreaHeight(0,-1,parseInt(he));
			}else if(oldv != newv && oldv - newv < 0) {//增加
				he = (newv - oldv);
				curhe = (parseInt($('div22').dheight) +he);		
				setPrintAreaHeight(0,1,parseInt(he));
			}//alert(he);
			if(oldv != newv) {			
			$('div22').style.height = curhe+'px';
			$('div22').dheight = curhe;			
			try{pdc.drawline($('line1'),1);}catch(e){}
			try{pdc.drawline($('line2'),2);}catch(e){}
			$('linetop').from = '0,'+value;
			$('linetop').to = $('div22').clientWidth+','+value;
			}
		//}
	} else if(4 == pagetype) {//下
		/*if(value < 20) {
			alert('设置值只能是大于20像素');
			obj.focus();
			obj.select();
		} else {*/
			var oldv = $('div22').dbottom;
			var he = 0;
			var curhe = 0;
			if(oldv > parseInt(value)) {
				he = oldv - parseInt(value);
				curhe = (parseInt($('div22').dheight) -he);
			} else {
				he = parseInt(value) - oldv;
				curhe = (parseInt($('div22').dheight) +he);					
			}
			$('div22').dbottom = value;
			$('div22').style.height = curhe+'px';
			$('div22').dheight = curhe;
			try{pdc.drawline($('line1'),1);}catch(e){}
			try{pdc.drawline($('line2'),2);}catch(e){}
		//}
	}else if(5==pagetype) {//左
		/*if(value < 20) {
			alert('设置值只能是大于20像素');
			obj.focus();
			obj.select();
		} else {*/
			$('div22').dleft = value;
			try{pdc.drawline($('line1'),1);}catch(e){}
		//}
	}else if(6==pagetype) {
		/*if(value < 20) {
			alert('设置值只能是大于20像素');
			obj.focus();
			obj.select();
		} else {*/
			$('div22').dright = value;
			try{pdc.drawline($('line2'),2);}catch(e){}
		//}
	}
}


/**
 * 动态创建控件
 */
function createControl(){
	if(!tempcontrol){
		tempcontrol=document.createElement("input");
		tempcontrol.style.display="none";
		tempcontrol.style.position="absolute";
		tempcontrol.style.cursor="default";
		tempcontrol.style.backgroundColor="#ffffff";
		tempcontrol.style.paddingBottom="0px";
		tempcontrol.style.width = '10px';
		tempcontrol.style.height  ='10px';
		tempcontrol.style.border = '1px  solid';
		tempcontrol.control=1;		
		document.body.appendChild(tempcontrol);
	}
	return tempcontrol;
}
//创建临时控件
function createTempLabel(obj) {
	var tempobj = createControl();
	tempobj.style.display = 'block';
	tempobj.style.top = event.y-5;
	tempobj.style.left = event.x-5;
	tempobj.style.filter="alpha(opacity=90)";	
	document.fireEvent('onmousedown');
	//var parentobj =document.elementFromPoint(event.x,event.y);
	//alert(parentobj);
}
//创建控件
function createLabel() {
	if(static_label) {//静态文本
		var x = event.x;
		var y = event.y;//alert(document.elementFromPoint(event.x,event.y).id);
		var parentobj = document.elementFromPoint(event.x,event.y);//$('div22');
		if(parentobj.id.substring(0,5) != 'title') return;		
		var label = document.createElement("input");
		var tempx = event.x - $('div1').offsetWidth;
		var tempy = eval(event.y - parentobj.offsetTop)-10-30;
		if(parentobj.offsetHeight<20) {
			alert('控件所在区域低于控件高度,请调整区域高度');
			static_label = false;
			return;
		}
		label.style.width = '100px';
		label.style.height = '18px';
		label.style.zIndex = 1000;
		label.style.border = '0.25mm #aca899 dotted';		
		label.statictext = '1';	
		label.style.left = tempx+'px';
		label.style.top =tempy+'px';//alert(tempy);
		label.style.fontSize = 12;
		label.style.textAlign = 'left';
		label.style.fontFamily = '宋体';
		label.valign = 'top';
		label.onresize = 'pdc.nodrags();';
		label.style.filter="alpha(opacity=50)";	
		label.oncontrolselect = 'addControlPros();';
		label.onresizestart = 'drawtempline(this)';
		//alert(event.x - $('div1').offsetWidth);
		parentobj.appendChild(label);//alert(label.outerHTML);
		label.style.position="absolute";
		if(parseInt(label.style.top.gsub('px','')) < 0) label.style.top = 0;
		if(parseInt(label.style.left.gsub('px','')) < 0) label.style.left = 0;
		label.attachEvent('onresize',pdc.nodrags);
		label.attachEvent('oncontrolselect',addControlPros);		
		
		controlPositionChange(label,parentobj);		
		static_label = false;
	}
	if(dynamic_text) {//动态文本
		var x = event.x;
		var y = event.y;//alert(document.elementFromPoint(event.x,event.y).id);
		var parentobj = document.elementFromPoint(event.x,event.y);//$('div22');
		if(parentobj.id.substring(0,5) != 'title') return;		
		var label = document.createElement("input");
		var tempx = event.x - $('div1').offsetWidth;
		var tempy = eval(event.y - parentobj.offsetTop)-10-30;
		if(parentobj.offsetHeight<20) {
			alert('控件所在区域低于控件高度,请调整区域高度');
			dynamic_text = false;
			return;
		}
		label.style.width = '100px';
		label.style.height = '18px';
		label.style.zIndex = 1000;
		label.style.border = '0.2mm #800000 dashed';		
		label.dynamictext = '1';	
		label.style.left = tempx+'px';
		label.style.top =tempy+'px';//alert(tempy);
		label.style.fontSize = 12;
		label.style.fontFamily = '宋体';
		label.style.textAlign = 'left';
		label.valign = 'top';
		label.classtype = 'java.lang.String';
		label.onresize = 'pdc.nodrags();';
		label.style.filter="alpha(opacity=50)";	
		label.oncontrolselect = 'addControlPros();';
		label.onresizestart = 'drawtempline(this)';
		//alert(event.x - $('div1').offsetWidth);
		parentobj.appendChild(label);//alert(label.outerHTML);
		label.style.position="absolute";
		if(parseInt(label.style.top.gsub('px','')) < 0) label.style.top = 0;
		if(parseInt(label.style.left.gsub('px','')) < 0) label.style.left = 0;
		label.attachEvent('onresize',pdc.nodrags);
		label.attachEvent('oncontrolselect',addControlPros);		
		
		controlPositionChange(label,parentobj);		
		dynamic_text = false;
	}
	if(child_print) {//明细		
		var x = event.x;
		var y = event.y;//alert(document.elementFromPoint(event.x,event.y).id);
		var parentobj = document.elementFromPoint(event.x,event.y);//$('div22');
		if(parentobj.id.substring(0,5) != 'title') return;		
		var label = document.createElement("input");
		var tempx = event.x - $('div1').offsetWidth;
		var tempy = eval(event.y - parentobj.offsetTop)-10-30;
		if(parentobj.offsetHeight<20) {
			alert('控件所在区域低于控件高度,请调整区域高度');
			dynamic_text = false;
			return;
		}
		label.style.width = '300px';
		label.style.height = '100px';
		label.readOnly = true;
		label.style.border = '0 none';		
		label.style.zIndex = 1000;		
		label.childprint = '1';	
		label.style.backgroundColor='#dfdfdf';
		label.style.left = tempx+'px';
		label.style.top =tempy+'px';//alert(tempy);
		label.onresize = 'pdc.nodrags();';
		label.style.filter="alpha(opacity=50)";	
		label.oncontrolselect = 'addControlPros();';
		//创建明细步骤
		var url = '/' +xrbs.WEB_APP_NAME  +  '/print/addChild.jsp?';
		var subinfo = showModalDialog(url+xr_ModalDialog_flag,window,"edge:raised;scroll:0;status:no;help:0;resizable:1;dialogHeight:300px;dialogWidth:500px;");
		if(subinfo ==null || subinfo =='') return;
		var subprintid = getAttribute(subinfo,'subprintid');
		var subprintname = getAttribute(subinfo,'subprintname');
		label.subprintid = subprintid;
		label.subprintname = subprintname;
		var subprint = getAttribute(subinfo,'parexp');
		label.subprint = subprint;
		parentobj.appendChild(label);//alert(label.outerHTML);
		label.style.position="absolute";
		if(parseInt(label.style.top.gsub('px','')) < 0) label.style.top = 0;
		if(parseInt(label.style.left.gsub('px','')) < 0) label.style.left = 0;
		label.attachEvent('onresize',pdc.nodrags);
		label.attachEvent('oncontrolselect',addControlPros);		
		
		controlPositionChange(label,parentobj);		
		child_print = false;
	}
	
	if(print_image) {//图片	
		var x = event.x;
		var y = event.y;//alert(document.elementFromPoint(event.x,event.y).id);
		var parentobj = document.elementFromPoint(event.x,event.y);//$('div22');
		if(parentobj.id.substring(0,5) != 'title') return;		
		var label = document.createElement("img");
		var tempx = event.x - $('div1').offsetWidth;
		var tempy = eval(event.y - parentobj.offsetTop)-10-30;
		if(parentobj.offsetHeight<20) {
			alert('控件所在区域低于控件高度,请调整区域高度');
			print_image = false;
			return;
		}
		label.border = '0';			
		label.printimage = '1';	
		label.style.width = '50px';
		label.style.height = '50px';
		label.style.left = tempx+'px';
		label.style.top =tempy+'px';//alert(tempy);
		label.onresize = 'pdc.nodrags();';
		//label.style.filter="alpha(opacity=50)";	
		label.oncontrolselect = 'addControlPros();';
		//创建图片步骤
		var url = '/' +xrbs.WEB_APP_NAME  +  '/print/addImage.jsp?';
		var imgsrc = showModalDialog(url+xr_ModalDialog_flag,window,"edge:raised;scroll:0;status:no;help:0;resizable:1;dialogHeight:200px;dialogWidth:380px;");
		if(imgsrc ==null || imgsrc =='') return;
		imgsrc = imgsrc.split('@@');
		//alert(imgsrc);		
		label.imgname = imgsrc[0];		
		if(imgsrc.length >1) {
			label.src = '/' +xrbs.WEB_APP_NAME + '/print/upimgfile/bar.jpg';
			label.barpic = 1;
		} else {
			label.src = '/' +xrbs.WEB_APP_NAME + '/print/upimgfile/' +imgsrc[0];
			label.barpic = 0;
		}		
		parentobj.appendChild(label);//alert(label.outerHTML);
		label.style.position="absolute";		
		if(parseInt(label.style.top.gsub('px','')) < 0) label.style.top = 0;
		if(parseInt(label.style.left.gsub('px','')) < 0) label.style.left = 0;
		label.attachEvent('onresize',pdc.nodrags);
		label.attachEvent('oncontrolselect',addControlPros);		
		
		controlPositionChange(label,parentobj);		
		print_image = false;
	}
}
//控件位置调整
function controlPositionChange(obj,parentobj) {
	try{//alert(parentobj);
		var supperobj = $('div22');
		//判断是否超出边界
		var x1 = parseInt(supperobj.dleft);
		var x2 = parentobj.clientWidth - parseInt(supperobj.dright);
		var y1 = 0;
		var y2 = parentobj.clientHeight;
		var x = obj.offsetLeft;
		var y = obj.offsetTop;
		//alert(x1 + '   ' + x2 + '  ' + y1+'  '+y2+'   '+x+'   '+y);
		
		if(x < x1) x = x1;
		else if(x > x2-obj.offsetWidth) x = (x2-obj.offsetWidth);
		if(y < y1) y = y1;
		else if((y+obj.offsetHeight) > y2) y = (y2-obj.offsetHeight+1);
		//alert(x +'   '+ y);
		obj.style.top = y;
		obj.style.left = x;	
		//防止超区域高度
		if(obj.offsetHeight > parentobj.clientHeight) {
			obj.style.top = 0;
			obj.style.height = parentobj.clientHeight+1;
		}
		if(obj.offsetWidth > (x2 -x1)) {
			obj.style.left = x1;
			obj.style.width = (x2-x1);
		}//alert(event.clientX + '  ' + event.x);
		//重设垂直对齐方式
		setValign(obj,obj.valign);

	}catch(e){}
}

//加载控件属性
function addControlPros() {
	var obj =  event.srcElement;
	curstaticlabel = obj;//alert(obj.outetHTML);
	if(obj.statictext == '1' || obj.dynamictext =='1') {
		var url = '/'+ xrbs.WEB_APP_NAME + '/print/labelText.jsp?';		
		var para = 'text='+obj.value.gsub(/[+]/,'_and_');
		para += '&fontsize=' + obj.style.fontSize.gsub('px','');
		para += '&textAlign=' + obj.style.textAlign;
		para += '&valign='+obj.valign;
		para += '&borderTopWidth='+(obj.borderTopWidth?obj.borderTopWidth:0);
		para += '&borderLeftWidth='+(obj.borderLeftWidth?obj.borderLeftWidth:0);
		para += '&borderRightWidth='+(obj.borderRightWidth?obj.borderRightWidth:0);
		para += '&borderBottomWidth='+(obj.borderBottomWidth?obj.borderBottomWidth:0);
		para += '&fontFamily=' + (obj.style.fontFamily==''?'宋体':obj.style.fontFamily);
		para += '&labeltype=' + ('1' == obj.statictext?'1':('1' == obj.dynamictext?'2':''));//alert(para);
		para += '&width=' + obj.offsetWidth;
		para += '&height=' + obj.offsetHeight;
		if(obj.dynamictext =='1') {
			para += '&classtype=' + (obj.classtype);
		}else {
			para += '&classtype=string';
		}
		para = encodeURI(para);
		para = encodeURI(para);		
		//document.all.controlproframe.src = url+para;		
		new _AJAX.Request(url, {method: 'post',parameters: para, onComplete:function(r) {
			$('div32').innerHTML = r.responseText;
			$('div32').innerHTML.evalScripts();
			xrBsMask.maskAllchild($('div32'));
		}});
	} else if(obj.childprint == '1') {
		var url = '/'+ xrbs.WEB_APP_NAME + '/print/childPropers.jsp?';		
		var para = 'printid=' +obj.subprintid;
		para += '&printname=' +(obj.subprintname?obj.subprintname:'');
		para = encodeURI(para);
		para = encodeURI(para);	
		new _AJAX.Request(url, {method: 'post',parameters: para, onComplete:function(r) {
			$('div32').innerHTML = r.responseText;
		}});
	} else if(obj.printimage  == '1') {
		var url = '/'+ xrbs.WEB_APP_NAME + '/print/imagepros.jsp?';	
		var para = 'imagename=' + obj.imgname;
		para += '&width=' + obj.offsetWidth;
		para += '&height=' + obj.offsetHeight;
		para += '&barpic=' + obj.barpic;
		para = encodeURI(para);
		para = encodeURI(para);	
		new _AJAX.Request(url, {method: 'post',parameters: para, onComplete:function(r) {
			$('div32').innerHTML = r.responseText;
		}});
	}
}
//控件属性改变后事件
function changecontrolpro(obj,type) {
	var tempobj = curstaticlabel;
	var value = parseInt(obj.value.gsub('_',''));
	if(tempobj) {
		if(type == 1) {//标签内容
			tempobj.value = obj.value;
		} else if(type == 2) {//字体大小
			tempobj.style.fontSize = value;
		} else if(type == 3) {//水平对齐
			tempobj.style.textAlign= obj.value;
		} else if(type == 4) {//垂直对齐
			tempobj.valign = obj.value;
			setValign(tempobj,obj.value);
		} else if(type == 5) {//上边框
			if(obj.checked) {
				tempobj.borderTopWidth = 1;
				tempobj.style.borderTopStyle = 'solid';
			} else {
				tempobj.borderTopWidth = 0;
				tempobj.style.borderTopStyle = 'dotted';
			}
		}else if(type == 6) {//下边框
			if(obj.checked) {
				tempobj.borderBottomWidth = 1;
				tempobj.style.borderBottomStyle = 'solid';
			} else {
				tempobj.borderBottomWidth = 0;
				tempobj.style.borderBottomStyle = 'dotted';
			}
		}else if(type == 7) {//左边框
			if(obj.checked) {
				tempobj.borderLeftWidth = 1;
				tempobj.style.borderLeftStyle = 'solid';
			} else {
				tempobj.borderLeftWidth = 0;
				tempobj.style.borderLeftStyle = 'dotted';
			}
		}else if(type == 8) {//右边框
			if(obj.checked) {
				tempobj.borderRightWidth = 1;
				tempobj.style.borderRightStyle = 'solid';
			} else {
				tempobj.borderRightWidth = 0;
				tempobj.style.borderRightStyle = 'dotted';
			}
		} else if(type == 9) {//字号
			tempobj.style.fontFamily = value;
		} else if(type == 10) {//类名
			tempobj.classtype = obj.value;
		} else if(type == 11) {//宽度
			tempobj.style.width = value;
		} else if(type == 12) {//高度
			tempobj.style.height = value;
		} else if(type == 81) {//图片宽度
			tempobj.style.width = value;
		} else if(type == 82) {//图片高度
			tempobj.style.height = value;
		}
		//tempobj.setActive();
	}
}
//修改图片
function modifyImg(obj) {
	var url = '/' +xrbs.WEB_APP_NAME  +  '/print/addImage.jsp?imgname=' +obj.value+'&barpic=' + obj.barpic;
	var imgsrc = showModalDialog(url+xr_ModalDialog_flag,window,"edge:raised;scroll:0;status:no;help:0;resizable:1;dialogHeight:200px;dialogWidth:380px;");
	if(imgsrc ==null || imgsrc =='') {return;}	
	else {
		imgsrc = imgsrc.split('@@');
		if(imgsrc.length >1) {
			curstaticlabel.src = '/' +xrbs.WEB_APP_NAME + '/print/upimgfile/bar.jpg';
			obj.barpic = 1;
		} else {
			curstaticlabel.src = '/' +xrbs.WEB_APP_NAME + '/print/upimgfile/' +imgsrc[0];
			obj.barpic = 0;
		}				
		curstaticlabel.imgname = imgsrc[0];
		obj.value = imgsrc[0];
		
	}
}
//设置垂直居中
function setValign(obj,value) {
	try {
	var sel = obj.createTextRange();
	var hei = sel.boundingHeight;
	if(value == 'middle') {
		obj.style.paddingTop = (obj.clientHeight - hei)/2;
	} else if(value == 'top') {
		obj.style.paddingTop = 0;
	} else if(value == 'bottom') {
		obj.style.paddingTop = (obj.clientHeight - hei);
	}
	}catch(e){}
}

//添加标签内容
function addLagelText(obj) {
	var url = '/' +xrbs.WEB_APP_NAME  +  '/print/addLabelText.jsp?';
	showModalDialog(url+xr_ModalDialog_flag,window,"edge:raised;scroll:0;status:no;help:0;resizable:1;dialogHeight:300px;dialogWidth:400px;");
}

//设置明细表参数
function setChildPars() {	
	var url = '/' +xrbs.WEB_APP_NAME  + '/print/childParameterSet.jsp?';	
	showModalDialog(url+xr_ModalDialog_flag,window,"edge:raised;scroll:0;status:no;help:0;resizable:1;dialogHeight:300px;dialogWidth:400px;");
}
function F2beforeClose() {
	var tempobj = curstaticlabel;
	tempobj.subprintid = $F('subprintid');
	tempobj.subprintname = $F('subprintname');
}
//************************************************************************************
//保存打印
function saveprint() {
	if($F('printname').replace(/^\s+|\s+$/g,"") =='') {
		alert('打印名称不能为空');
		$('printname').focus();
		return;
	}
	var url = '/' +xrbs.WEB_APP_NAME  +  '/PrintAction.do?method=savePrint';
	var para = 'name=' + $F('printname').replace(/^\s+|\s+$/g,"");
	para += '&querydata='+$('querydata').value.gsub(/[#]/,'@jing@').gsub(/[+]/,'@jia@').gsub(/[%]/,'@beifen@').gsub(/&/,'@and@');
	para += '&content=' + $('div2').innerHTML.gsub(/[#]/,'@jing@').gsub(/[+]/,'@jia@').gsub(/[%]/,'@beifen@').gsub(/&/,'@and@');
	para += '&parameters='+$('parameters').value;//alert(para);
	if($('id').value != '') {
		para += '&id=' + $('id').value;
		url = '/' +xrbs.WEB_APP_NAME  +  '/PrintAction.do?method=updatePrint';
	}
	///alert($('id').value);//return;
	para = encodeURI(para);
	para = encodeURI(para);
	var method =  null;
	if(arguments.length == 1 && (typeof arguments[0] == 'function'))  {
		method=arguments[0];
	}
	new _AJAX.Request(url, {method: 'post',parameters: para, onComplete: function(r) {
		var text = r.responseText.split('@@@');//alert(text);
		if(text[0].replace(/^\s+|\s+$/g,"") != '') {
		 $('id').value=text[0].replace(/^\s+|\s+$/g,"");
		}		
		if(method) method.call(window);			
		else alert(text[1]);
	}});
}

/**
 * 打印单据
 * @param filename 报表文件名(*.jrxml)
 * @param paravalue 参数值串(param1=value1;param2=value2;...),param为报表中所定义参数
 * @param paratype 参数类型串(param1=type1;param2=type2;...),param为报表中所定义参数,type=1为数字,type=2为日期,type=3为字符
 * @param bs_url 打印后需要访问的链接
 * @return
 */
function printReport(filename,paravalue,paratype,bs_url) {
	bs_url = bs_url.replace("?","_@@_").replace(/&/g,"!p!");
	var parainfo = "&parainfo="+paravalue;
	parainfo = parainfo.gsub(' ','blankspace');
	
	var url = "/"+xrbs.WEB_APP_NAME+"/sysmanager/print/viewAppletPrinter.jsp?&filename="+filename+parainfo+"&paratype="+paratype+"&bs_url="+bs_url;	
	//alert(url+xr_ModalDialog_flag);
	showModalDialog(url+xr_ModalDialog_flag,self,"edge:raised;scroll:0;status:1;help:0;resizable:1;dialogWidth:850px;dialogHeight:900px;");
}
//预览打印
function viewrint() {
	saveprint(viewtransfermethod);
	
}
//预览调用方法
function viewtransfermethod() {
	var id = $('id').value;
	if(id == '') alert('打印文件生成错误');
	var parinfo = $('parameters').value.split(';');
	var para = '';
	var paratype = '';
	for(var i = 0; i < parinfo.length;i++) {
		if(parinfo[i].replace(/^\s+|\s+$/g,"") != '') {
			var pname = parinfo[i].split(':')[0];
			var ptype = parinfo[i].split(':')[1];
			var pvalue = prompt('请输入'+pname + '的值','');		
			para += pname +'='+pvalue+";";
			paratype += (pname +'='+(ptype.indexOf('BigDecimal') == -1?"3":"1")+";");
			//alert(parinfo[i].split(':'));
		}
	}
	//alert(para + '   ' + paratype);
	printReport('print'+id+'.jasper',para,paratype,'');
}
